/* Copyright (C) 2007-2008  klevgert  http://code.google.com/p/db4o-netbeans/

This file is part of the db4o-netbeans module suite.

db4o-netbeans is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.
 
db4o-netbeans is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

package com.db4o.nb.wizards;

import com.db4o.nb.util.ExtensionFileFilter;
import java.io.File;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;

/**
 *
 * @author  klevgert
 */
public class Db4oTemplateComponent extends javax.swing.JPanel {
  
  /**
   * Creates new form Db4oTemplateComponent
   */
  public Db4oTemplateComponent() {
    initComponents();
  }
  
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  private void initComponents() {
    pnlDatabaseFile = new javax.swing.JPanel();
    lblFilename = new javax.swing.JLabel();
    txtFilename = new javax.swing.JTextField();
    btnBrowseFile = new javax.swing.JButton();
    pnlDatabaseServer = new javax.swing.JPanel();
    lblHost = new javax.swing.JLabel();
    txtHost = new javax.swing.JTextField();
    lblPort = new javax.swing.JLabel();
    txtPort = new javax.swing.JTextField();
    lblUser = new javax.swing.JLabel();
    txtUser = new javax.swing.JTextField();
    cmbConnectionType = new javax.swing.JComboBox();
    jLabel1 = new javax.swing.JLabel();
    lblPassword = new javax.swing.JLabel();
    txtPassword = new javax.swing.JPasswordField();

    setName("Connection Details");
    lblFilename.setText("File Name:");

    btnBrowseFile.setText("...");
    btnBrowseFile.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnBrowseFileActionPerformed(evt);
      }
    });

    org.jdesktop.layout.GroupLayout pnlDatabaseFileLayout = new org.jdesktop.layout.GroupLayout(pnlDatabaseFile);
    pnlDatabaseFile.setLayout(pnlDatabaseFileLayout);
    pnlDatabaseFileLayout.setHorizontalGroup(
      pnlDatabaseFileLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(pnlDatabaseFileLayout.createSequentialGroup()
        .addContainerGap()
        .add(lblFilename)
        .add(22, 22, 22)
        .add(txtFilename, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(btnBrowseFile, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 35, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addContainerGap())
    );
    pnlDatabaseFileLayout.setVerticalGroup(
      pnlDatabaseFileLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(pnlDatabaseFileLayout.createSequentialGroup()
        .addContainerGap()
        .add(pnlDatabaseFileLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(btnBrowseFile)
          .add(txtFilename, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
          .add(lblFilename))
        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    lblHost.setText("Host:");

    txtHost.setEnabled(false);

    lblPort.setText("Port:");

    txtPort.setEnabled(false);

    lblUser.setText("User:");

    txtUser.setEnabled(false);

    org.jdesktop.layout.GroupLayout pnlDatabaseServerLayout = new org.jdesktop.layout.GroupLayout(pnlDatabaseServer);
    pnlDatabaseServer.setLayout(pnlDatabaseServerLayout);
    pnlDatabaseServerLayout.setHorizontalGroup(
      pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(pnlDatabaseServerLayout.createSequentialGroup()
        .addContainerGap()
        .add(pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(lblHost)
          .add(lblPort)
          .add(lblUser))
        .add(46, 46, 46)
        .add(pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(txtPort, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
          .add(txtHost, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
          .add(txtUser, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE))
        .addContainerGap())
    );
    pnlDatabaseServerLayout.setVerticalGroup(
      pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(pnlDatabaseServerLayout.createSequentialGroup()
        .addContainerGap()
        .add(pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(lblHost)
          .add(txtHost, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
          .add(lblPort)
          .add(txtPort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(lblUser)
          .add(txtUser, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    cmbConnectionType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "File", "Server" }));
    cmbConnectionType.setName("connectionType");
    cmbConnectionType.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cmbConnectionTypeActionPerformed(evt);
      }
    });

    jLabel1.setText("Connection:");

    lblPassword.setText("Password:");

    txtPassword.setToolTipText("Optionally enter the password for connection");

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
          .add(org.jdesktop.layout.GroupLayout.LEADING, pnlDatabaseServer, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
            .add(jLabel1)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(cmbConnectionType, 0, 378, Short.MAX_VALUE))
          .add(org.jdesktop.layout.GroupLayout.LEADING, pnlDatabaseFile, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .add(layout.createSequentialGroup()
            .add(lblPassword)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 23, Short.MAX_VALUE)
            .add(txtPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 367, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(jLabel1)
          .add(cmbConnectionType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(pnlDatabaseFile, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(pnlDatabaseServer, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(txtPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
          .add(lblPassword))
        .addContainerGap(34, Short.MAX_VALUE))
    );
  }// </editor-fold>//GEN-END:initComponents

  private void cmbConnectionTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbConnectionTypeActionPerformed

    JComboBox cb = (JComboBox)evt.getSource();
    String selectedItem = (String)cb.getSelectedItem();
    updateStates(selectedItem.equals("File"));
    
  }//GEN-LAST:event_cmbConnectionTypeActionPerformed

  private void btnBrowseFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseFileActionPerformed

    // Request file name from user
    JFileChooser fileChooser = new JFileChooser(".");
    int status = fileChooser.showOpenDialog(null);
    if (status == JFileChooser.CANCEL_OPTION ) {
      return;
    }      
    File selectedFile = fileChooser.getSelectedFile();
    String fileName = selectedFile.getAbsolutePath();
    
    getTxtFilename().setText(fileName);
  }//GEN-LAST:event_btnBrowseFileActionPerformed
  
  private void updateStates(boolean fileSelected) {
    
    this.pnlDatabaseFile.setEnabled(fileSelected);
    this.lblFilename.setEnabled(fileSelected);
    this.getTxtFilename().setEnabled(fileSelected);

    this.pnlDatabaseServer.setEnabled(!fileSelected);
    this.lblHost.setEnabled(!fileSelected);
    this.getTxtHost().setEnabled(!fileSelected);
    this.lblPort.setEnabled(!fileSelected);
    this.getTxtPort().setEnabled(!fileSelected);
    this.lblUser.setEnabled(!fileSelected);
    this.getTxtUser().setEnabled(!fileSelected);
//    this.getTxtPassword().setEnabled(!fileSelected);    

  }  

  public javax.swing.JTextField getTxtFilename() {
    return txtFilename;
  }

  public javax.swing.JTextField getTxtHost() {
    return txtHost;
  }

  public javax.swing.JPasswordField getTxtPassword() {
    return txtPassword;
  }

  public javax.swing.JTextField getTxtPort() {
    return txtPort;
  }

  public javax.swing.JTextField getTxtUser() {
    return txtUser;
  }

  public javax.swing.JComboBox getCmbConnectionType() {
    return cmbConnectionType;
  }
  
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btnBrowseFile;
  private javax.swing.JComboBox cmbConnectionType;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel lblFilename;
  private javax.swing.JLabel lblHost;
  private javax.swing.JLabel lblPassword;
  private javax.swing.JLabel lblPort;
  private javax.swing.JLabel lblUser;
  private javax.swing.JPanel pnlDatabaseFile;
  private javax.swing.JPanel pnlDatabaseServer;
  private javax.swing.JTextField txtFilename;
  private javax.swing.JTextField txtHost;
  private javax.swing.JPasswordField txtPassword;
  private javax.swing.JTextField txtPort;
  private javax.swing.JTextField txtUser;
  // End of variables declaration//GEN-END:variables
  
}
